package com.tinckay.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.entity.po.Storehouse;
import com.tinckay.service.StorehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.api.ApiController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-21
 */
@Api(tags = "仓体管理")
@RestController
@RequestMapping("/storehouse")
public class StorehouseController extends ApiController {

    @Autowired
    private StorehouseService storehouseService;

    @Log("删除仓体")
    @DeleteMapping
    @ApiOperation("删除仓体")
    //@RequiresPermissions("storehouse:delete")
    public R delete(@ApiParam("仓体code") @RequestParam String code) {
        storehouseService.removeById(code);
        return success(null);
    }

    @Log("新增仓体")
    @PostMapping
    @ApiOperation("新增仓体")
    //@RequiresPermissions("storehouse:add")
    public R add(@RequestBody @Validated(ProjectConstant.Save.class) Storehouse storehouse) {
        if (storehouse.getType() != 0) {
            if (storehouse.getRightHeight() == null) {
                return failed("右高不能为空");
            }
            if (storehouse.getRightHeight() <= storehouse.getHeight()) {
                return failed("该类型仓体，右高需大于左高");
            }
        }
        storehouseService.save(storehouse);
        return success(storehouse);
    }

    @Log("更新仓体")
    @PutMapping
    @ApiOperation("更新仓体")
    //@RequiresPermissions("storehouse:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) Storehouse storehouse) {
        storehouseService.updateById(storehouse);
        return success(storehouse);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询仓体列表")
    //@RequiresPermissions("storehouse:list")
    public R<IPage> list(
            @RequestParam(required = false) String code,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        IPage<Storehouse> page = storehouseService.page(new Page<>(current, size),
                new QueryWrapper<Storehouse>().lambda().eq(StrUtil.isNotBlank(code), Storehouse::getCode, code));
        return success(page);
    }

    @GetMapping("/info/{code}")
    @ApiOperation("获取仓体信息")
    //@RequiresPermissions("storehouse:info")
    public R<Storehouse> info(@PathVariable("code") String code) {
        return success(storehouseService.getOne(new QueryWrapper<Storehouse>().lambda().eq(Storehouse::getCode, code)));
    }

    @GetMapping("/findUnPumpCode")
    @ApiOperation("模糊查询未浇筑仓体code")
    //@RequiresPermissions("storehouse:findUnPumpCode")
    public R findUnPumpCode(@RequestParam(required = false) String code) {
        List<String> codes = storehouseService.findCode(code);
        return success(codes);
    }

}

